package com.api.syh56.shengyunhui.service.impl;

import com.api.syh56.shengyunhui.common.BaseUserInfo;
import com.api.syh56.shengyunhui.common.Constants;
import com.api.syh56.shengyunhui.common.UserInfoEnu;
import com.api.syh56.shengyunhui.entity.*;
import com.api.syh56.shengyunhui.service.*;
import com.api.syh56.shengyunhui.util.LocationUtils;
import com.api.syh56.shengyunhui.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Objects;

@Service
@Slf4j
public class WAGParamServiceImpl implements WAGParamService {
    @Autowired
    private WayBillService wayBillService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private AddressService addressService;
    @Autowired
    private TmpAddressService tmpAddressService;

    @Autowired
    private UserService userService;

    @Override
    public Waybill insertWAG(WAGParam wagParam) {

        Long uid = Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode()));
        Waybill waybill = wagParam.getWaybill();

        int expectedDeliveryDays = waybill.getExpectedDeliveryDays();
        if (Objects.isNull(expectedDeliveryDays)) {
            throw new RuntimeException("预计送达天数不能为空");
        }
        //计算预计送达时间
        waybill.setExpectedDeliveryTime(LocalDateTime.now().plusDays(expectedDeliveryDays));

        //货物信息

        Goods goods = wagParam.getGoods();
        System.out.println("货物尺寸:"+goods.getGoodsWeight());
//        goodsService.insert(goods);
        goodsService.save(goods);
        //货物id
        waybill.setGoodsId(goods.getId());

        //发布时默认为报价0
        waybill.setOfferEnd(BigDecimal.valueOf(0));
        waybill.setOfferInit(BigDecimal.valueOf(0));
        waybill.setOfferAll(BigDecimal.valueOf(0));

//        Long shipAddressId = wagParam.getShip_addressId();
//        Long receiveAddressId = wagParam.getReceive_addressId();
//        Address shipAddress = addressService.selectById(shipAddressId);
//        Address receiveAddress = addressService.selectById(receiveAddressId);

//        TmpAddress shiptmpAddress = new TmpAddress();
//        TmpAddress receivetmpAddress = new TmpAddress();
        TmpAddress shiptmpAddress = wagParam.getShip_address();
        TmpAddress receivetmpAddress = wagParam.getReceive_address();

        shiptmpAddress.setUid(uid);
        receivetmpAddress.setUid(uid);
//        BeanUtils.copyProperties(shipAddress,shiptmpAddress);
//        BeanUtils.copyProperties(receiveAddress,receivetmpAddress);
        CommonUtil.setIdToNull(shiptmpAddress);
        CommonUtil.setIdToNull(receivetmpAddress);
        tmpAddressService.insert(shiptmpAddress);
        tmpAddressService.insert(receivetmpAddress);

        //将临时地址id插入waybill
        waybill.setShipAddressId(shiptmpAddress.getId());
        waybill.setReceiveAddressId(receivetmpAddress.getId());

        //运单状态：待报价
        waybill.setWaybillStatus(Constants.WaybillStatus.AWAIT_QUOTE);
//        waybill.setWaybillStatus(Constants.WaybillStatus.WAIT_QUOTE);
        //运输状态：默认
        waybill.setDeliveryStatus(Constants.DeliveryStatus.DEFAULT);

        //计算发货市和收货市经纬度的总路程
        String shipAddressLocation = shiptmpAddress.getAddressLocation();
        String receiveAddressLocation = receivetmpAddress.getAddressLocation();
        // 解析经纬度字符串
        double[] shipLocation = LocationUtils.parseLocation(shipAddressLocation);
        double[] receiveLocation = LocationUtils.parseLocation(receiveAddressLocation);
        // 计算距离
        double distance = LocationUtils.calculateDistance(shipLocation[0], shipLocation[1],
                receiveLocation[0], receiveLocation[1]);

        log.info("Total Distance: " + distance + " km");
        waybill.setTotalDistance(distance);

        waybill.setOriAdCode(shiptmpAddress.getAdCode());
        waybill.setDstAdCode(receivetmpAddress.getAdCode());
        //拼接Adcode
        waybill.setOriAndDstAdCode(shiptmpAddress.getAdCode() + "," + receivetmpAddress.getAdCode());

        wayBillService.addWaybill(waybill);
        return waybill;
    }
}
